<?php
namespace app\web\admin;

use app\common\builder\ZBuilder;
use app\web\model\Coupon as myModel;
use app\web\model\CouponType;
use app\web\model\User;



/**
 * 轮播管理（后台）
 * @package
 */
class Coupon extends Base 
{
    public $status = ['可用','已使用','已过期'];
    public function index()
    {
        $map = $this->getMap();
        foreach ($map as $key => $value) {
            if($value[0] == 'type_name') $map[$key][0] = 'c.type_name';
            if($value[0] == 'status') $map[$key][0] = 'a.status';
        }
        $list = myModel::alias('a')
                        ->leftjoin('user b','b.id=a.user_id')
                        ->leftjoin('coupon_type c','c.id=a.type_id')
                        ->field('a.*,b.nickName as user_id,c.type_name as type_id')
                        ->where($map)
                        ->order($this->getOrder('id desc'))
                        ->paginate();

        return ZBuilder::make('table')
            ->setTableName('coupon')
             ->hideCheckbox()
            ->setSearch(['type_name' => '优惠券名称']) // 设置搜索参数
            ->addColumns([
                ['user_id','用户昵称'],
                ['type_id', '优惠券名称'],
                ['money', '面值'],
                ['full_money','满N元可使用'],
                ['validity_time','有效时间'],
                ['status', '状态', 'status','',$this->status],
                ['use_time','使用时间','datetime']
            ])
            ->addOrder('money,full_money,validity_time')
            ->setRowList($list)
            ->addFilterList('user_id',User::getSelectAll('nickName<>""','nickName'))
            ->addFilterList('type_id',CouponType::getSelectAll(null,'type_name'))
            ->addFilterList('status',$this->status) 
            ->fetch();
    }
}